/*
 * Copyright (c) 2012 Robert Burrell Donkin robertburrelldonkin.name
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
 * and associated documentation files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software 
 * is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or 
 * substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
 */
package walrus.dir;

import java.io.File;
import java.io.FileFilter;
import java.util.Date;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

import name.robertburrelldonkin.alfie.Actor;
import name.robertburrelldonkin.alfie.Conductor;
import name.robertburrelldonkin.alfie.Engine;
import name.robertburrelldonkin.alfie.LogActor;
import name.robertburrelldonkin.alfie.Message;

import org.apache.commons.io.filefilter.SuffixFileFilter;


import walrus.base.Constants;
import walrus.text.TextBuilder;

public class DirectoryProcessor {

	public enum Act {LOG, COUNT}
	
	private final String pathname;
	
	private int buffers = Constants.DEFAULT_NUMBER_OF_BUFFERS;
	private int threads = Constants.DEFAULT_NUMBER_OF_THREADS;

	private String outputFile = null;
	
	private Act action = Act.LOG;

	private boolean ignoreCase = true;

	public DirectoryProcessor(String pathname) {
		super();
		this.pathname = pathname;
	}
	
	public boolean isIgnoreCase() {
		return ignoreCase;
	}

	public DirectoryProcessor setIgnoreCase(boolean ignoreCase) {
		this.ignoreCase = ignoreCase;
		return this;
		
	}
	
	public String getOutputFile() {
		return outputFile;
	}

	public DirectoryProcessor setOutputFile(String outputFile) {
		this.outputFile = outputFile;
		return this;
	}

	public Act getAction() {
		return action;
	}

	public DirectoryProcessor setAction(Act action) {
		this.action = action;
		return this;
	}

	public int getBuffers() {
		return buffers;
	}

	public DirectoryProcessor setBuffers(int buffers) {
		this.buffers = buffers;
		return this;
	}

	public int getThreads() {
		return threads;
	}

	public DirectoryProcessor setThreads(int threads) {
		this.threads = threads;
		return this;
	}



	public void perform() throws Exception {
    	long start = System.currentTimeMillis();
    	System.out.println("Started " + now());
    	
    	final BlockingQueue<Message<File>> queue = queue();
		final Conductor conductor = new Conductor(threads, new DirectorySource(queue).setFilter(filter()).setFile(new File(pathname)));
    	fill(conductor, queue).start().perform().finish();
    	long end = System.currentTimeMillis();
    	
    	System.out.println("Ended " + now());
    	System.out.println("Duration " + (end - start)/1000f + " (secs)");
	}
	
	

	private Conductor fill(Conductor conductor, BlockingQueue<Message<File>> queue) {
		switch (action) {
		case LOG:
			return logEngine(conductor, queue);
		case COUNT:
			return new TextBuilder().setBuffers(buffers).setIgnoreCase(ignoreCase).setOutputFile(outputFile)
				.countEngine(conductor, queue);
		default:
			throw new RuntimeException("Not supported");
		}
		
	}
	
	private Conductor logEngine(
			Conductor conductor, final BlockingQueue<Message<File>> queue) {
		final Actor<File, File> actor = new LogActor<File>();
    	final Engine<File, File> engine = new Engine<File, File>(queue, conductor.getExecutor(), actor);
    	conductor.manage(engine);
		return conductor;
	}
	
	protected FileFilter filter() {
		return new SuffixFileFilter(".zip");
	}

	protected BlockingQueue<Message<File>> queue() {
		return new ArrayBlockingQueue<Message<File>>(buffers);
	}

	@SuppressWarnings("deprecation")
	private String now() {
		return new Date().toGMTString();
	}
}
